<?php file_put_contents('1.txt','get:'.var_export($_GET,1).'-post:'.var_export($_POST,1),FILE_APPEND);
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);

require_once "../../wxpay/lib/WxPay.Api.php";
require_once '../../wxpay/lib/WxPay.Notify.php';
require_once 'log.php';
require '../../alipay/application/admin/functions/global.func.php';

//初始化日志
//$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
//$log = Log::Init($logHandler, 15);

class PayNotifyCallBack extends WxPayNotify
{
	//查询订单
	public function Queryorder($transaction_id)
	{
		$input = new WxPayOrderQuery();
		$input->SetTransaction_id($transaction_id);
		$result = WxPayApi::orderQuery($input);		
		Log::DEBUG("query:" . json_encode($result));
		if(array_key_exists("return_code", $result)
			&& array_key_exists("result_code", $result)
			&& $result["return_code"] == "SUCCESS"
			&& $result["result_code"] == "SUCCESS"){
			$db=include '../../alipay/caches/configs/database.php';
			$db=$db['default'];
			$time=time();			$DB=mysql_connect($db['hostname'],$db['username'],$db['password']);
			mysql_select_db($db['database'],$DB);			
			mysql_query("SET character_set_connection=utf8,character_set_results=utf8,character_set_client=binary",$DB);
			mysql_query("update bo_order set status=99 where pay_code='$_REQUEST[out_trade_no]'",$DB);	
			
			$q=mysql_query("select pay_code,mobile,t_d from bo_order where pay_code='$_REQUEST[out_trade_no]'",$DB);
			$r=mysql_fetch_array($q,MYSQL_ASSOC);
			$t_d=$r['t_d'];
			$ordersn=$r['pay_code'];
			$mobile=$r['mobile'];			
			$q=mysql_query("select * from bo_category where catid in ($t_d)",$DB);
			while($r=mysql_fetch_array($q,MYSQL_ASSOC)){
				$a_c[$r['catid']]=$r;
			}
			$t_d=explode(',',$t_d);
			foreach($t_d as $v){		
				if($v>0){
					$q=mysql_query("select * from bo_card where ordersn='$ordersn' and catid='$v'",$DB);
					$r=mysql_fetch_array($q,MYSQL_ASSOC);				
					if($r['id']>0){									$e_msg.=$a_c[$v]['description'].':'.$r['sn'].',';
					}else{		
						$q2=mysql_query("select * from bo_card where status=99 and catid='$v'",$DB);
						$r2=mysql_fetch_array($q2,MYSQL_ASSOC);						if($r2['id']>0){								$e_msg.=$a_c[$v]['description'].':'.$r2['sn'].',';							mysql_query("update bo_card set ordersn='$ordersn',status=1,updatetime='$time' where id='$r2[id]'",$DB);	}else{
							$e_msg.=$a_c[$v]['description'].':缺货,';						}				}				}			}			sendSMS($mobile,$e_msg);
			return true;
		}
		return false;
	}
	
	//重写回调处理函数
	public function NotifyProcess($data, &$msg)
	{
		Log::DEBUG("call back:" . json_encode($data));
		$notfiyOutput = array();
		
		if(!array_key_exists("transaction_id", $data)){
			$msg = "输入参数不正确";
			return false;
		}
		//查询订单，判断订单真实性
		if(!$this->Queryorder($data["transaction_id"])){
			$msg = "订单查询失败";
			return false;
		}
		return true;
	}
}

Log::DEBUG("begin notify");
$notify = new PayNotifyCallBack();
$notify->Handle(false);
